package simulateOS.management.device;

import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.Label;

import javafx.scene.layout.VBox;
import org.springframework.stereotype.Service;
import simulateOS.controller.DeviceController;
import simulateOS.entity.Device;
import simulateOS.management.computerInterface.DeviceInterface;

import javax.annotation.Resource;


/**
 * 设备的请求方法，和设备使用完后的归还方法
 */
@Service
public class DeviceManager implements DeviceInterface {
    public static int[] aDevice = new int[2];
    public static int[] bDevice = new int[3];
    public static int[] cDevice = new int[3];

    private VBox deviceBox;

    @Resource
    private DeviceController deviceController;

    static {
        int i;

        for(i = 0; i < 2; ++i) {
            aDevice[i] = 0;
        }

        for(i = 0; i < 3; ++i) {
            bDevice[i] = 0;
        }

        for(i = 0; i < 3; ++i) {
            cDevice[i] = 0;
        }
    }

    /**
     * 请求设备
     */
    @Override
    public Device applyDevice(String deviceName, int pid) {
        int deviceType = Device.getDeviceIndexByName(deviceName);

        Device device = new Device();
        device.setDeviceName(deviceName);

        if(deviceBox == null){
            deviceBox = deviceController.getDeviceBox();
        }
        int i = 0;
        if (deviceType == 0) {
            while (i < 2 && aDevice[i] != 0) {
                ++i;
            }

            if (i == 2) {
                device.setDeviceId(-1);
            } else {
                aDevice[i] = pid;
                refreshDevice(deviceType, pid);
                device.setDeviceId(i);
            }
        } else if (deviceType == 1) {
            while (i < 3 && bDevice[i] != 0) {
                ++i;
            }

            if (i == 3) {
                device.setDeviceId(-1);
            } else {
                bDevice[i] = pid;
                refreshDevice(deviceType, pid);
                device.setDeviceId(i);
            }
        } else if (deviceType != 2) {
            device.setDeviceId(-1);
        } else {
            while (i < 3 && cDevice[i] != 0) {
                ++i;
            }

            if (i == 3) {
                device.setDeviceId(-1);
            } else {
                cDevice[i] = pid;
                refreshDevice(deviceType, pid);
                device.setDeviceId(i);
            }
        }
        return device;
    }

    /**
     * 归还设备
     */
    @Override
    public void retrieveDevice(Device device) {

        int deviceType = Device.getDeviceIndexByName(device.getDeviceName());
        int deviceId = device.getDeviceId();

        if (deviceType == 0) {
            aDevice[deviceId] = 0;
            refreshDevice(0, -1);
        } else if (deviceType == 1) {
            bDevice[deviceId] = 0;
            refreshDevice(1, -1);
        } else if (deviceType == 2) {
            cDevice[deviceId] = 0;
            refreshDevice(2, -1);
        }

    }

    /**
     * 更新设备的视图
     */
    public void refreshDevice(int deviceType, int pid) {
        // 提交线程任务
        Platform.runLater(new RefreshDeviceTask(deviceType,pid));
    }
    public void setDeviceBox(VBox deviceBox) {
        this.deviceBox = deviceBox;
    }

    public void setDeviceAppController(DeviceController deviceController) {
        this.deviceController = deviceController;
    }

    /**
     * 刷新设备管理视图的异步任务
     */
    private class RefreshDeviceTask extends Task<Object>{

        private int deviceType;
        private int pid;

        public RefreshDeviceTask(int deviceType, int pid) {
            this.deviceType = deviceType;
            this.pid = pid;
        }

        @Override
        protected Object call() throws Exception {
            if(deviceBox == null){
                deviceBox = deviceController.getDeviceBox();
            }
            if (deviceType == 0) {
                for (int i = 0; i < 2; i++) {
                    if (DeviceManager.aDevice[i] == 0) {
                        deviceBox.getChildren().get(i).setId("emptyBox");
                        ((Label) deviceBox.getChildren().get(i)).setText("设备A：空闲");
                    } else if (pid != -1){
                        deviceBox.getChildren().get(i).setId("deviceInBox");
                        ((Label) deviceBox.getChildren().get(i)).setText("设备A：#" + pid);
                    }
                }
            } else if (deviceType == 1) {
                for (int i = 0; i < 3; i++) {
                    if (DeviceManager.bDevice[i] == 0) {
                        deviceBox.getChildren().get(2 + i).setId("emptyBox");
                        ((Label) deviceBox.getChildren().get(2 + i)).setText("设备B：空闲");
                    } else if (pid != -1){
                        deviceBox.getChildren().get(2 + i).setId("deviceInBox");
                        ((Label) deviceBox.getChildren().get(2 + i)).setText("设备B：#" + pid);
                    }
                }
            } else {
                for (int i = 0; i < 3; i++) {
                    if (DeviceManager.cDevice[i] == 0) {
                        deviceBox.getChildren().get(5 + i).setId("emptyBox");
                        ((Label) deviceBox.getChildren().get(5 + i)).setText("设备C：空闲");

                    } else if (pid != -1){
                        deviceBox.getChildren().get(5 + i).setId("deviceInBox");
                        ((Label) deviceBox.getChildren().get(5 + i)).setText("设备C：#" + pid);
                    }
                }
            }
            return null;
        }
    }
}
